<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="寄件编码" prop="sendId">
        <el-input
          v-model="queryParams.sendId"
          placeholder="请输入寄件编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出库编码" prop="outboundId">
        <el-input
          v-model="queryParams.outboundId"
          placeholder="请输入出库编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请寄件" prop="sendStatus">
        <el-select v-model="queryParams.sendStatus" placeholder="请选择申请寄件" clearable>
          <el-option
            v-for="dict in dict.type.send_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="寄件时间" prop="sendTime">
        <el-date-picker clearable
          v-model="queryParams.sendTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择寄件时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="寄件人" prop="loginAccount">
        <el-input
          v-model="queryParams.loginAccount"
          placeholder="请输入寄件人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:sending:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:sending:edit']"
        >申请寄件</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:sending:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:sending:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sendingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="寄件编码" align="center" prop="sendId" />
      <el-table-column label="出库编码" align="center" prop="outboundId" />
      <el-table-column label="订单编号" align="center" prop="orderNumber" />
      <el-table-column label="申请寄件" align="center" prop="sendStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.send_status" :value="scope.row.sendStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="寄件时间" align="center" prop="sendTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="寄件人" align="center" prop="loginAccount" />
      <el-table-column label="客户名称" align="center" prop="userName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:sending:edit']"
          >寄件</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:sending:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改寄件申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="寄件编码" prop="sendId">
          <el-input v-model="form.sendId" placeholder="请输入寄件编码" :disabled = "true" />
        </el-form-item>
        <el-form-item label="出库编码" prop="outboundId">
          <el-input v-model="form.outboundId" placeholder="请输入出库编码" :disabled = "true" />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入订单编号" :disabled = "true" />
        </el-form-item>
        <el-form-item label="申请寄件" prop="sendStatus">
          <el-select v-model="form.sendStatus" placeholder="请选择申请寄件" >
            <el-option
              v-for="dict in dict.type.send_status"
              :key="dict.value"
              :label="dict.label"
:value="parseInt(dict.value)"
              :disabled = "true"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="寄件时间" prop="sendTime">
          <el-date-picker clearable
            v-model="form.sendTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择寄件时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="寄件人" prop="loginAccount">
          <el-input v-model="form.loginAccount" placeholder="请输入寄件人" :disabled = "true" />
        </el-form-item>
        <el-form-item label="客户名称" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入客户名称" :disabled = "true" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSending, getSending, delSending, addSending, updateSending } from "@/api/system/sending";
import {addClearance} from "@/api/system/clearance";
import {listInfo, updateInfo} from "@/api/system/info";

export default {
  name: "Sending",
  dicts: ['send_status'],
  data() {
    return {
      //订单单列表
      orderList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 寄件申请表格数据
      sendingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sendId: null,
        outboundId: null,
        orderNumber: null,
        sendStatus: null,
        sendTime: null,
        loginAccount: null,
        userName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sendId: [
          { required: true, message: "寄件编码不能为空", trigger: "blur" }
        ],
        outboundId: [
          { required: true, message: "出库编码不能为空", trigger: "blur" }
        ],
        orderNumber: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
        sendStatus: [
          { required: true, message: "申请寄件不能为空", trigger: "change" }
        ],
        userName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询寄件申请列表 */
    getList() {
      this.loading = true;
      listSending(this.queryParams).then(response => {
        this.sendingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

      listInfo().then(res => {
        this.orderList = res.rows;
      });

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sendId: null,
        outboundId: null,
        orderNumber: null,
        sendStatus: null,
        sendTime: null,
        loginAccount: null,
        userName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加寄件申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSending(id).then(response => {
        this.form = response.data;
        if(this.form.sendStatus === 0){//0 -已申请 1 - 未申请
          this.$modal.msgWarning("该订单已申请寄件成功");
        }else{
          this.form.sendStatus = 0;
          this.form.loginAccount = this.$store.state.user.name;
          this.open = true;
          this.title = "寄件申请";
        }


      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSending(this.form).then(response => {

              const dataForm = {
                id: this.form.id,
                sendId: this.form.sendId,
                orderNumber: this.form.orderNumber,
                customsClearanceTime: null,
                customsClearanceStatus: 1,
                stocktakingStatus: 1,
                stocktakingTime: null
              };
              addClearance(dataForm);

              this.orderList.forEach((item, index) => {
                if(item.orderNumber === this.form.orderNumber){
                  const orderForm = {
                    id: item.id,
                    orderNumber: item.orderNumber,
                    orderType: item.orderType,
                    orderStatus: item.orderStatus,
                    orderCreateTime: item.orderCreateTime,
                    storeStatus: item.storeStatus,
                    storeTime: item.storeTime,
                    packStatus: item.outboundStatus,//打包
                    packTime: item.outboundTime,
                    outboundStatus: item.outboundStatus,//出库
                    outboundTime: item.outboundTime,
                    stocktakingStatus: item.stocktakingStatus,//盘点状态
                    stocktakingTime: item.stocktakingTime,
                    sendStatus: this.form.sendStatus,
                    sendTime: this.form.sendTime,
                    userName: item.userName
                  };
                  updateInfo(orderForm);
                }

              });


              this.$modal.msgSuccess("寄件成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSending(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除寄件申请编号为"' + ids + '"的数据项？').then(function() {
        return delSending(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sending/export', {
        ...this.queryParams
      }, `sending_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
